#pragma once

#include <iostream>
#include <fstream>
#include <string>
#include <cstdarg>
#include <ctime>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>



enum{
    Debug = 0,
    Info,//普通
    Warning,
    Error,
    Fatal
};
enum
{
    Screen = 10,
    OneFile,
    ClassFile
};

std::string LevelToString(int level)
{
    switch (level)
    {
    case Debug:
        return "Debug";
    case Info:
        return "Info";
    case Warning:
        return "Warning";
    case Error:
        return "Error";
    case Fatal:
        return "Fatal";
    default:
        return "Unknown";
    }
};

const int defaultstyle = Screen;
const std::string default_filename = "log.";
const std::string logdir = ".log";//日志后缀名

class Log
{
public:
    Log():style(defaultstyle),filename(default_filename){
        mkdir(logdir.c_str(), 0755);
    }
    void Enable(int sty){
        style = sty;
    }
    std::string GetCurrentTime()//获取时间
    {
        time_t currtime = time(nullptr);
        struct tm* curr = localtime(&currtime);
        char time_buffer[128];
        snprintf(time_buffer, sizeof(time_buffer), "%d-%d-%d %d:%d:%d",
                 curr->tm_year + 1900, curr->tm_mon + 1, curr->tm_mday,
                 curr->tm_hour, curr->tm_min, curr->tm_sec);
        return time_buffer;
    }
        void WriteLogToOneFile(const std::string& logname, const std::string& message)
    {
        umask(0);//给予全部权利
        int fd = open(logname.c_str(),O_CREAT |O_WRONLY|O_APPEND, 0666);
        if(fd < 0)
        {
            return ;
        }
        write(fd, message.c_str(), message.size());
        close(fd);
    }
    void WriteLogToClassFile(const std::string &levelstr, const std::string &message)
    {
        std::string logname = logdir;
        logname += "/";
        logname += filename;
        logname += levelstr;//  eg. log/log.Info log/log.all
        WriteLogToOneFile(logname, message);
    }
    void WriteLog(const std::string& levelstr, const std::string& message)
    {
        switch (style)//选择模式
        {
        case Screen:
            std::cout << message;
            break;
        case OneFile:
            WriteLogToClassFile("all", message);
            break;
        case ClassFile:
            WriteLogToClassFile(levelstr, message);
            break;
        default:
            break;
        }
    }
    void LogMessage(int level, const char* format, ...)
    {
        char leftbuffer[1024];
        std::string levelstr = LevelToString(level);//模式Info/Fatal
        std::string currtime = GetCurrentTime();//获取现在时间 
        std::string idstr = std::to_string(getpid());//获取进程id

        char rightbuffer[1024];
        va_list args;
        va_start(args, format);
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, args);//将参数列表写入右缓存
        va_end(args);
        //日记
        snprintf(leftbuffer,sizeof(leftbuffer), "[%s][%s][%s]", levelstr.c_str(), currtime.c_str(), idstr.c_str());
        std::string loginfo = leftbuffer;
        loginfo += rightbuffer;//loginfo:level+time+pid+参数列表
        WriteLog(levelstr ,loginfo);
    }
private:
    int style;
    std::string filename;
};
Log lg;

class Conf
{
public:
    Conf()
    {
        lg.Enable(Screen);
    }
    ~Conf()
    {}
};

Conf conf;